package com.pan.controller.Notices;

import com.pan.aop.BusinessType;
import com.pan.aop.MyLog;
import com.pan.controller.tools.Methods;
import com.pan.controller.tools.R1;
import com.pan.domain.Notice;
import com.pan.domain.User;
import com.pan.service.INoticeService;
import com.pan.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/noticeoperaters")
@Tag(name = "NoticeOperaterController",description = "操作员控制公告类")
public class NoticeOperaterController {
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IUserService userService;

    Methods methods = new Methods();

    //操作人员获取所有公告(包括删除)
    @Operation(summary = "操作员获取所有公告",description = "包括已删除的")
    @GetMapping
    @MyLog(title = "操作人员获取所有公告",optParam = "无",businessType = BusinessType.SELECT)
    public R1 getAllNotice(HttpServletRequest request){
        //检查用户权限
        if (methods.isYanfa(request,userService)){
            return new R1(true,noticeService.getAllNote());
        }
        return new R1(false,"权限不足");
    }

    //根据id获取公告(包括删除)
    @Operation(summary = "操作员逻辑删除公告",description = "'1'为未删除 '0'为已删除")
    @GetMapping("/{id}")
    @MyLog(title = "操作人员根据ID获取公告",optParam = "#{id}",businessType = BusinessType.SELECT)
    public R1 getById(@Parameter(description = "编号",required = true,example = "1") @PathVariable(value = "id") Integer id ,
                      HttpServletRequest request){
        //检查用户权限
        if (methods.isYanfa(request,userService)){
            return new R1(true,noticeService.getById(id));
        }
        return new R1(false,"权限不足");
    }


    //操作人员添加公告
    @Operation(summary = "操作人员添加公告",description = "发布者和发布时间不在前端页面获取")
    @PostMapping
    @MyLog(title = "操作人员添加公告",optParam = "#{notice}",businessType = BusinessType.INSERT)
    public R1 putNewNotice(@Parameter(description = "通知数据，这是一个Json字符串",required = true) @RequestBody Notice notice,
                           HttpServletRequest request){
        //检查用户权限
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        //先查询出包含部门编号的所有信息
        User user2 = userService.getByUsername(user1.getUsername());
        //然后通过部门编号查询部门权限
        String aty = userService.getByDeptno(user2.getDeptno());

        if (!aty.equals("研发")){
            return new R1(false,"权限不足");
        }

        //指定发布者
        notice.setCreator(user2.getName());
        //指定发布时间
        Date datetime = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        notice.setCreateTime(sbf.format(datetime));
        return new R1(true,noticeService.save(notice));
    }

    //操作人员修改公告
    @Operation(summary = "操作人员修改公告",description = "信息可动态提交")
    @PutMapping
    @MyLog(title = "操作人员修改公告",optParam = "#{notice}",businessType = BusinessType.UPDATE)
    public R1 changeNotice(@Parameter(description = "新的公告信息,为Json字符串",required = false) @RequestBody Notice notice,
                           HttpServletRequest request){
        //检查用户权限
        if(methods.isYanfa(request,userService)){
            return new R1(true,noticeService.updateByIdE(notice));
        }
        return new R1(false,"权限不足");
    }
}
